package com.senon.mvpretrofitrx.mvp.presenter;

import android.content.Context;

import com.senon.mvpretrofitrx.mvp.contract.NewsContract;
import com.senon.mvpretrofitrx.mvp.model.NewsModel;
import com.senon.mvpretrofitrx.mvp.progress.ObserverResponseListener;
import com.senon.mvpretrofitrx.mvp.test.BaseResponse;
import com.senon.mvpretrofitrx.mvp.test.NewsResult;
import com.senon.mvpretrofitrx.mvp.utils.ExceptionHandle;
import com.senon.mvpretrofitrx.mvp.utils.ToastUtil;

import java.util.HashMap;

/**
 * Created by Administrator on 2018/4/18.
 */

public class NewsPresenter extends NewsContract.Presenter {

    public NewsModel newsModel;
    private Context context;

    public NewsPresenter(Context context) {
        this.context = context;
        this.newsModel = new NewsModel();
    }

    @Override
    public void getNews(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        newsModel.getNewsList(context, map, isDialog, cancelable, getView().bindLifecycle(), new ObserverResponseListener() {
            @Override
            public void onNext(Object o) {
                //这一步是必须的，判断view是否已经被销毁
                if (getView() != null) {
                    getView().result((BaseResponse<NewsResult>) o);
                    getView().setMsg("请求成功");
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                if (getView() != null) {
                    ToastUtil.showShortToast(ExceptionHandle.handleException(e).message);
                }
            }
        });
    }
}
